import Axios from "../../lib/request";
import uploadFile from "./upload";

export const getAllService = function() {
	return Axios.get("/dicAfterservice/list");
};

export const getAgentLevel = function() {
	return Axios.get("/level/getDicLevel");
};

let parentList = [];
const get1CategoryList = function() {
	return Axios.get("/category/list1");
};

const get2CategoryList = function(data) {
	return Axios.get("/category/list2", { params: data });
};

const forEachParent = function(list, callback) {
	list.forEach((item, index) => {
		let param = { id: item.value };
		get2CategoryList(param).then(res2 => {
			console.log(res2.data);
			let tempList = res2.data || [];
			let list2 = [];
			list2 = tempList.map(item2 => {
				return { label: item2.name, value: item2.id, image: item2.image || "" };
			});
			callback(list2, index);
		});
	});
};

export const getCategoryList = function(callback) {
	get1CategoryList().then(res => {
		let list = res.data || [];
		parentList = list.map(item => {
			return { label: item.name, value: item.id, children: [] };
		});
		forEachParent(parentList, function(data, index) {
			parentList[index].children = data;
		});
		callback(parentList);
	});
};

export const uploadFileFunction = function(obj, callback) {
	if (!obj) return;
	uploadFile(obj, callback);
};
